home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / bindings / fun / spell < prev    next >
Encoding:
Text File  |  1996-09-27  |  22.9 KB  |  651 lines

  1. @if-using not(ocl-file-spell)
  2.    @use (ocl-file-spell)
  3.    ;OCL{{{}}}
  4.    ;OCL{{{  libs
  5.    @if-using not(ocl-file-delchar) @lib delchar @fi
  6.    ;OCL}}}
  7.    ;OCL{{{  reference description
  8.    @if-using ( FUN-REFERENCE ) ; 1 ;
  9.    This library defines a function, which uses the output  of  `spell'  to
  10.    start an intelligent interactive search and replace.
  11.  
  12.    If the checked file is read only, the  spelling  errors  will  only  be
  13.    listed in a new buffer. In  this  case, the search and replace will not
  14.    be started!
  15.  
  16.    If you do not want to use the standard spellchecker `spell', change the
  17.    value of history `spell-command' to your spell checker.
  18.  
  19.    This package can only be  used,  if  Origami  is  compiled  to  support
  20.    regular expressions.
  21.    @fi
  22.    ;OCL}}}
  23.    ;OCL{{{  variables
  24.    ( defvar
  25.       ( spelled-buffer
  26.         spell-out-buff
  27.         spell-op
  28.         spell-len
  29.         spell-replace-set
  30.         spell-empty-replace
  31.         spell-x
  32.         spell-old-regexp-mode
  33.         spell-view
  34.         spell-mark-word
  35.         spell-mark-used
  36.         spell-pause
  37.         spell-error-line-mark
  38.       )
  39.    )
  40.    ( history spell-command )
  41.    ( history spell-menu )
  42.    ( history spell-replaces )
  43.    ;OCL}}}
  44.    ;OCL{{{  default-spell-command
  45.    @if-using not( NEW-DEF-SPELL )
  46.       ( defmac default-spell-command ( "spell ) )
  47.    @fi
  48.    ;OCL}}}
  49.    ;OCL{{{  special basic regular expression chars
  50.    @if-using not( basic-regexp-chars-defined )
  51.       @use ( basic-regexp-chars-defined )
  52.       ( defset basic-regexp-chars ( "* ". "[ "] "^ "$ "\ ) )
  53.    @fi
  54.    ;OCL}}}
  55.    ;OCL{{{  spell-basic-magic
  56.    ;OCL{{{  define string for missing re
  57.    ( demand-load not (
  58.       ( defmac no-reg-exp
  59.          ( "Sorry, "  "no "  "regular "  "expressions! )
  60.       )
  61.       ( undeclare ( no-reg-exp ) )
  62.    ) )
  63.    ;OCL}}}
  64.    ( defmac spell-basic-magic
  65.       ( delete-mode-regular-expression
  66.         set spell-old-regexp-mode +(1 ocl-argument)
  67.         add-mode-basic-regular-expression
  68.         if =(ocl-argument 0)
  69.          ( message-exit ( "[ "Sorry, "  "no "  "regular "  "expressions! "] ) )
  70.         fi
  71.       )
  72.    )
  73.    ;OCL}}}
  74.    ;OCL{{{  spell-restore-magic
  75.    ( defmac spell-restore-magic
  76.       ( case
  77.          ( pre
  78.             ( set spell-old-regexp-mode -(spell-old-regexp-mode 1) )
  79.               =(spell-old-regexp-mode 0)
  80.             ( delete-mode-regular-expression )
  81.          )
  82.          ( pre
  83.             ( set spell-old-regexp-mode -(spell-old-regexp-mode 1) )
  84.               =(spell-old-regexp-mode 0)
  85.             ( add-mode-basic-regular-expression )
  86.          )
  87.          ( pre
  88.             ( set spell-old-regexp-mode -(spell-old-regexp-mode 1) )
  89.               =(spell-old-regexp-mode 0)
  90.             ( add-mode-extended-regular-expression )
  91.          )
  92.         default
  93.          ( set spell-old-regexp-mode 0 )
  94.         esac
  95.       )
  96.    )
  97.    ;OCL}}}
  98.    ;OCL{{{  spell-end
  99.    ( deffun spell-end
  100.       ( if =(current-buffer-id spell-out-buff)
  101.          ( if spell-mark-used
  102.             ;OCL{{{  remove trailing words and rename buffer
  103.             ( screen-off
  104.               while not(test-bottom)
  105.                ( delete-to-end-of-line
  106.                  next-line
  107.                  delete-previous-character
  108.                )
  109.               previous-line
  110.               delete-previous-character
  111.               set-file-unchanged
  112.               beginning-of-fold
  113.               next-line
  114.               beginning-of-line
  115.               change-buffername
  116.                  "marked-spell-errors
  117.                  newline-and-indent
  118.               screen-on
  119.               redraw-display
  120.             )
  121.             ;OCL}}}
  122.            else
  123.             ;OCL{{{  exit
  124.             ( set-file-unchanged
  125.               exit-buffer
  126.             )
  127.             ;OCL}}}
  128.            fi
  129.          )
  130.         fi
  131.         set spell-out-buff 0
  132.       )
  133.    )
  134.    ;OCL}}}
  135.    ;OCL{{{  spell-buffer
  136.    ;OCL{{{  reference
  137.    @if-using ( FUN-REFERENCE ) ; 2 ; spell-buffer-ref
  138.    ;OCL{{{  @spell-mac-ref spell-buffer
  139.    Start a interactive search and replace for all misspelled words.  If
  140.    you used the Edit facility, the function tries to continue  the  old
  141.    spell check!
  142.    ;OCL}}}
  143.    @fi
  144.    ;OCL}}}
  145.    ;OCL{{{  spell-menu-string
  146.    ;OCL{{{  define sub strings for menus
  147.    ( demand-load not(
  148.       ( defmac first-menu-part
  149.          ( "Ok "  "Next "  "Replace "  )
  150.       )
  151.       ( defmac mid-menu-part
  152.          ( "Substitue "( get-history spell-replaces 0 ") "  "All "   )
  153.       )
  154.       ( defmac last-menu-part
  155.          ( "Edit if =(spell-mark-word 0) ( "  "  "Mark ) fi "  "  "  "Quit  )
  156.       )
  157.    ) )
  158.    ;OCL}}}
  159.    ( deffun spell-menu-string
  160.       ( history-edit spell-menu ()
  161.            first-menu-part
  162.            if spell-replace-set
  163.             ( mid-menu-part )
  164.            fi
  165.            last-menu-part
  166.            newline-and-indent
  167.       )
  168.    )
  169.    ;OCL{{{  undeclare sub strings
  170.    ( undeclare
  171.       ( first-menu-part
  172.         mid-menu-part
  173.         last-menu-part
  174.       )
  175.    )
  176.    ;OCL}}}
  177.    ;OCL}}}
  178.    ( deffun spell-buffer
  179.       (
  180.         ;OCL{{{  check if pausing spell can be restarted
  181.         set spell-op current-buffer-id
  182.         screen-off
  183.         if
  184.          ;OCL{{{  spell-pause and cannot restore handling
  185.          and
  186.             ( spell-pause
  187.               or
  188.                ( <>(spelled-buffer current-buffer-id)
  189.                  pre
  190.                   ( goto-buffer-id spell-out-buff )
  191.                     <>(current-buffer-id spell-out-buff)
  192.                  pre
  193.                   ( goto-line-mark spell-error-line-mark )
  194.                     test-bottom
  195.                )
  196.             )
  197.          ;OCL}}}
  198.          ;OCL{{{  stop old spelling
  199.          ( goto-buffer-id spell-out-buff
  200.            if =(spell-op spell-out-buff)
  201.             ( set spell-out-buff 0 )
  202.            fi
  203.            set spell-pause 0
  204.            spell-end
  205.          )
  206.          ;OCL}}}
  207.         fi
  208.         goto-buffer-id spell-op
  209.         screen-on
  210.         redraw-display
  211.         ;OCL}}}
  212.         if
  213.          ;OCL{{{  paused or normal-file and save succesfull
  214.            or
  215.             ( spell-pause
  216.               not
  217.                ( or
  218.                   ( <>(file-type 0)
  219.                     pre
  220.                      ;OCL{{{  exit folds and save
  221.                      (
  222.                        ;OCL{{{  exit entered folds
  223.                        while set-enter
  224.                         ( exit-fold )
  225.                        ;OCL}}}
  226.                        ;OCL{{{  clear message storage
  227.                        if
  228.                           or
  229.                            ( last-message M_CANTWRITE
  230.                              last-message M_DIR_EDIT
  231.                              last-message M_CLOSE_FAILED
  232.                            )
  233.                         ()
  234.                        fi
  235.                        ;OCL}}}
  236.                        save-file
  237.                      )
  238.                      ;OCL}}}
  239.                      ;OCL{{{  save successfull
  240.                        or
  241.                         ( last-message M_CANTWRITE
  242.                           last-message M_DIR_EDIT
  243.                           last-message M_CLOSE_FAILED
  244.                         )
  245.                      ;OCL}}}
  246.                   )
  247.                )
  248.             )
  249.          ;OCL}}}
  250.          ;OCL{{{  do the spell check
  251.          ( screen-off
  252.            spell-basic-magic
  253.            if not(spell-pause)
  254.             ;OCL{{{  get new info
  255.             ( set spell-mark-used false
  256.               ;OCL{{{  store filedata
  257.               set spelled-buffer current-buffer-id
  258.               set spell-view counter test-view
  259.               history-edit-filename spell-replaces ()
  260.                  newline-and-indent
  261.               ;OCL}}}
  262.               ;OCL{{{  open dev-null buffer
  263.               local
  264.                ( modify-behaviour )
  265.                ( set modify-behaviour -2
  266.                  open-buffer
  267.                )
  268.               ;OCL}}}
  269.               if <>(spelled-buffer current-buffer-id)
  270.                ;OCL{{{  get spell errors and handle them
  271.                ( set spell-out-buff current-buffer-id
  272.                  delete-mode-view
  273.                  ;OCL{{{  show minimized spell-check buffer
  274.                  repeat screen-height
  275.                   ( shrink-buffer )
  276.                  screen-on
  277.                  redraw-display
  278.                  ;OCL}}}
  279.                  ;OCL{{{  get spell info
  280.                  message ( M_READING "  "' history spell-command "  history spell-replaces "' )
  281.                  screen-off
  282.                  local ( force-bourne-shell )
  283.                   ( set force-bourne-shell true
  284.                     pipe-from-command
  285.                        get-history spell-command 0
  286.                        if test-begin-line
  287.                         ( default-spell-command )
  288.                        fi
  289.                        "  ;
  290.                        get-history spell-replaces 0
  291.                        newline-and-indent
  292.                   )
  293.                  beginning-of-fold
  294.                  next-line
  295.                  beginning-of-line
  296.                  center-and-redraw-display
  297.                  ;OCL}}}
  298.                  if ocl-argument
  299.                   ;OCL{{{  command failed, so quit this buffer
  300.                   ( spell-end )
  301.                   ;OCL}}}
  302.                  fi
  303.                )
  304.                ;OCL}}}
  305.               fi
  306.             )
  307.             ;OCL}}}
  308.            else
  309.             ;OCL{{{  restore old info
  310.             ( goto-buffer-id spell-out-buff
  311.               forward-character
  312.               forward-character
  313.             )
  314.             ;OCL}}}
  315.            fi
  316.            screen-on
  317.            redraw-display
  318.            if <>(spelled-buffer current-buffer-id)
  319.             ;OCL{{{  handle the errors
  320.             ( if not(spell-view)
  321.                ;OCL{{{  replace-loop
  322.                ( screen-off
  323.                  set spell-op 0
  324.                  while and(<>(spell-op "q) not(test-bottom))
  325.                   ;OCL{{{  handle word
  326.                   ( goto-buffer-id spell-out-buff
  327.                     ;OCL{{{  init search
  328.                     if spell-pause
  329.                      ;OCL{{{  clear old +/*, use old mark value
  330.                      ( beginning-of-line
  331.                        forward-character
  332.                        forward-character
  333.                        delete-previous-character
  334.                        delete-previous-character
  335.                      )
  336.                      ;OCL}}}
  337.                     else
  338.                      ;OCL{{{  start unmarked
  339.                      ( set spell-mark-word false )
  340.                      ;OCL}}}
  341.                     fi
  342.                     ;OCL{{{  get search pattern and its length
  343.                     set spell-len 0
  344.                     history-edit-line search ()
  345.                        beginning-of-line
  346.                        "\ "( "^\|[^[:alnum:]] "\ ")
  347.                        while not(test-end-line)
  348.                         ( if test-char-set basic-regexp-chars
  349.                            ( "\ )
  350.                           fi
  351.                           forward-character
  352.                           set spell-len +(spell-len 1)
  353.                         )
  354.                        "\ "( "$\|[^[:alnum:]] "\ ")
  355.                        newline-and-indent
  356.                     ;OCL}}}
  357.                     if spell-mark-word ( "* ) else ( "+ ) fi "  ;
  358.                     set spell-error-line-mark store-line-mark
  359.                     ;OCL}}}
  360.                     ;OCL{{{  init replace
  361.                     if not(spell-pause)
  362.                      ( set spell-replace-set false )
  363.                     fi
  364.                     ;OCL}}}
  365.                     ;OCL{{{  init menu
  366.                     spell-menu-string
  367.                     set spell-op 0
  368.                     ;OCL}}}
  369.                     ;OCL{{{  move to search start position in spelled buffer
  370.                     goto-buffer-id spelled-buffer
  371.                     if not(spell-pause)
  372.                      ( beginning-of-fold
  373.                        end-of-line
  374.                      )
  375.                     fi
  376.                     ;OCL}}}
  377.                     set spell-pause false
  378.                     ;OCL{{{  word handling loop
  379.                     local
  380.                      ()
  381.                      ( while
  382.                         ;OCL{{{  found pattern
  383.                           pre
  384.                            ( if last-message M_FAILED () fi
  385.                              search-forward previous-line newline-and-indent
  386.                            )
  387.                              not(last-message M_FAILED)
  388.                         ;OCL}}}
  389.                         (
  390.                           ;OCL{{{  move over leading non-char
  391.                           if not(test-char-set alnum)
  392.                            ( forward-character )
  393.                           fi
  394.                           ;OCL}}}
  395.                           if <>(spell-op "a)
  396.                            ;OCL{{{  get new command
  397.                            ( do
  398.                               (
  399.                                 ;OCL{{{  show commands and prompt for action
  400.                                 screen-on
  401.                                 redraw-display
  402.                                 message ( history spell-menu )
  403.                                 begin-prompt-macro
  404.                                 show-cursor 0
  405.                                 prompt-char-counter spell-op ()
  406.                                 end-prompt-macro
  407.                                 screen-off
  408.                                 ;OCL}}}
  409.                                 case
  410.                                  ;OCL{{{  q/Q/o/O -> break from word loop
  411.                                  ( =(spell-op "Q)
  412.                                     ( set spell-op "q
  413.                                       return-from-macro
  414.                                     )
  415.                                  )
  416.                                  ( or
  417.                                     ( =(spell-op "o) =(spell-op "O)
  418.                                       =(spell-op "q)
  419.                                     )
  420.                                     ( return-from-macro )
  421.                                  )
  422.                                  ;OCL}}}
  423.                                  ;OCL{{{  e/E -> edit, spell pause
  424.                                  ( or(=(spell-op "e) =(spell-op "E))
  425.                                     ( set spell-pause true
  426.                                       spell-restore-magic
  427.                                       screen-on
  428.                                       redraw-display
  429.                                       @if-using ( PRE-SPELL-EDIT-COMMAND )
  430.                                          pre-spell-edit-command
  431.                                       @fi
  432.                                       message-exit
  433.                                        ( "[ "spell
  434.                                          "  "suspended
  435.                                          "  "for
  436.                                          "  "editing
  437.                                          "]
  438.                                        )
  439.                                     )
  440.                                  )
  441.                                  ;OCL}}}
  442.                                  ;OCL{{{  m/M -> mark word
  443.                                  ( or(=(spell-op "m) =(spell-op "M))
  444.                                     ( set spell-op 0
  445.                                       if not(spell-mark-word)
  446.                                        ( set spell-mark-word true
  447.                                          set spell-mark-used true
  448.                                          spell-menu-string
  449.                                          ;OCL{{{  change marker + -> *
  450.                                          goto-buffer-id spell-out-buff
  451.                                          delete-previous-character
  452.                                          delete-previous-character
  453.                                          "* "  ;
  454.                                          set-file-unchanged
  455.                                          goto-buffer-id spelled-buffer
  456.                                          ;OCL}}}
  457.                                        )
  458.                                       fi
  459.                                     )
  460.                                  )
  461.                                  ;OCL}}}
  462.                                  ;OCL{{{  n/N -> continue loop
  463.                                  ( or(=(spell-op "n) =(spell-op "N))
  464.                                     ( forward-character )
  465.                                  )
  466.                                  ;OCL}}}
  467.                                  ;OCL{{{  r/R -> edit new rep and break prompt loop
  468.                                  ( or(=(spell-op "r) =(spell-op "R))
  469.                                     (
  470.                                       ;OCL{{{  get new replace
  471.                                       screen-on
  472.                                       history-edit spell-replaces ( M_REPLACE )
  473.                                       local
  474.                                        ()
  475.                                        ( do
  476.                                           ( begin-prompt-macro
  477.                                             prompt-char-counter spell-op ()
  478.                                             end-prompt-macro
  479.                                             if =(spell-op newline-and-indent)
  480.                                              ( return-from-macro )
  481.                                             fi
  482.                                             insert-ascii spell-op
  483.                                           )
  484.                                          while true
  485.                                        )
  486.                                       end-of-line
  487.                                       set spell-empty-replace counter test-begin-line
  488.                                       set spell-replace-set true
  489.                                       newline-and-indent
  490.                                       screen-off
  491.                                       ;OCL}}}
  492.                                       ;OCL{{{  buildt new menu
  493.                                       spell-menu-string
  494.                                       ;OCL}}}
  495.                                       set spell-op "s
  496.                                     )
  497.                                  )
  498.                                  ;OCL}}}
  499.                                  ;OCL{{{  no replace set -> loop
  500.                                  ( not(spell-replace-set) ( set spell-op 0 ) )
  501.                                  ;OCL}}}
  502.                                  ;OCL{{{  a/A -> break from prompt loop
  503.                                  ( =(spell-op "a) () )
  504.                                  ( =(spell-op "A) ( set spell-op "a ) )
  505.                                  ;OCL}}}
  506.                                  ;OCL{{{  s/S -> break from prompt loop
  507.                                  ( =(spell-op "s) () )
  508.                                  ( =(spell-op "S) ( set spell-op "s ) )
  509.                                  ;OCL}}}
  510.                                 default
  511.                                  ;OCL{{{  continue prompting
  512.                                  ( set spell-op 0 )
  513.                                  ;OCL}}}
  514.                                 esac
  515.                               )
  516.                              while =(spell-op 0)
  517.                            )
  518.                            ;OCL}}}
  519.                           fi
  520.                           if or(=(spell-op "s) =(spell-op "a))
  521.                            ;OCL{{{  replace pattern
  522.                            (
  523.                              ;OCL{{{  remove old word
  524.                              local
  525.                               ( spell-len )
  526.                               ( while and(>(spell-len 0) not(test-end-line))
  527.                                  ( forward-character
  528.                                    delete-previous-character
  529.                                    set spell-len -(spell-len 1)
  530.                                  )
  531.                               )
  532.                              ;OCL}}}
  533.                              ;OCL{{{  put new
  534.                              if not(spell-empty-replace)
  535.                               ( get-history spell-replaces 0 )
  536.                              fi
  537.                              ;OCL}}}
  538.                            )
  539.                            ;OCL}}}
  540.                           fi
  541.                         )
  542.                      )
  543.                     ;OCL}}}
  544.                     goto-buffer-id spell-out-buff
  545.                     if spell-mark-word
  546.                      ;OCL{{{  delete mark and move to next
  547.                      ( delete-previous-character
  548.                        delete-previous-character
  549.                        next-line
  550.                      )
  551.                      ;OCL}}}
  552.                     else
  553.                      ;OCL{{{  delete word
  554.                      ( backward-character
  555.                        backward-character
  556.                        delete-to-end-of-line
  557.                        next-line
  558.                        delete-previous-character
  559.                      )
  560.                      ;OCL}}}
  561.                     fi
  562.                     set-file-unchanged
  563.                   )
  564.                   ;OCL}}}
  565.                  spell-end
  566.                  screen-on
  567.                  redraw-display
  568.                )
  569.                ;OCL}}}
  570.               else
  571.                ;OCL{{{  stay buffer
  572.                ( end-of-fold
  573.                  set spell-mark-used true
  574.                  spell-end
  575.                )
  576.                ;OCL}}}
  577.               fi
  578.             )
  579.             ;OCL}}}
  580.            fi
  581.            spell-restore-magic
  582.            message ()
  583.          )
  584.          ;OCL}}}
  585.         fi
  586.       )
  587.    )
  588.    ;OCL{{{  undeclares
  589.    ( undeclare ( spell-menu-string ) )
  590.    ;OCL}}}
  591.    ;OCL}}}
  592.    ;OCL{{{  abort-hook-add
  593.    @if-using not(ABORT-HOOK-ADD)
  594.       @use (ABORT-HOOK-ADD)
  595.       ( defmac abort-hook-add ( ) )
  596.    @fi
  597.    ( defmac abort-hook-add-saved ( abort-hook-add ) )
  598.    ( undeclare ( abort-hook-add ) )
  599.    ( defmac abort-hook-add
  600.       ( abort-hook-add-saved
  601.         set modify-behaviour 0
  602.         spell-restore-magic
  603.         if and(spell-out-buff not(spell-pause))
  604.          ( goto-buffer-id spell-out-buff
  605.            if =(current-buffer-id spell-out-buff)
  606.             ( spell-end
  607.               set spell-out-buff 0
  608.             )
  609.            fi
  610.          )
  611.         fi
  612.       )
  613.    )
  614.    ( undeclare ( abort-hook-add-saved ) )
  615.    ;OCL}}}
  616.    ;OCL{{{  reference hooks
  617.    @if-using ( FUN-REFERENCE ) ; 3 ;
  618.    The libary defines a macro `abort-hook-add' and a OCL tag ABORT-HOOK-ADD.
  619.    This macro should be added to your abort-macro! If needed, an existing macro
  620.    `abort-hook-add' will be integrated!
  621.  
  622.    If you do not want to use `spell' as default spell checker, define the
  623.    OCL-tag `NEW-DEF-SPELL' and a function `default-spell-command' (the default
  624.    is `(defmac default-spell-command ( "spell ) )').
  625.  
  626.    If you have to change some internal states, when using the Edit facility,
  627.    define the OCL tag PRE-SPELL-EDIT-COMMAND and a function
  628.    pre-spell-edit-command.
  629.    @fi
  630.    ;OCL}}}
  631.    ;OCL{{{  undeclares
  632.    ( undeclare
  633.       ( spelled-buffer
  634.         spell-out-buff
  635.         spell-op
  636.         spell-len
  637.         spell-replace-set
  638.         spell-empty-replace
  639.         spell-x
  640.         spell-old-regexp-mode
  641.         spell-basic-magic
  642.         spell-restore-magic
  643.         spell-mark-used
  644.         spell-mark-word
  645.         spell-pause
  646.         spell-error-line-mark
  647.       )
  648.    )
  649.    ;OCL}}}
  650. @fi
  651.